package com.geekchique.runningbuddy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Menu.Item;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.CurrentRunMapControllerImpl;
import com.geekchique.runningbuddy.controller.ICurrentRunMapController;
import com.geekchique.runningbuddy.controller.IOptionsMenu;
import com.geekchique.runningbuddy.controller.OptionsMenuImpl;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.service.RemoteStopClockServiceInterface;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class CurrentRunMap extends MapActivity {

	private ICurrentRunMapController mapOfJogController;
	public LocationManager locationManager = null;
	protected MyIntentReceiver myIntentReceiver = null;
	final Handler mHandler = new Handler();
	private static final String TAG = CurrentRunMap.class.getName();
	private IOptionsMenu optionsMenu;

  @Override
  public void onCreate(Bundle  icicle) {
      super.onCreate(icicle);
      this.setContentView(R.layout.current_run_map_view);
      optionsMenu = new OptionsMenuImpl(this);

      this.myIntentReceiver = new MyIntentReceiver();

      mapOfJogController = new CurrentRunMapControllerImpl(this);

      mapOfJogController.createMapOfJogView((MapView)findViewById(R.id.map));
      
      mapOfJogController.startTrackingRoute();
  	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}

	public void stopStopwatchService(ServiceConnection connection) {
		mapOfJogController.setServiceRunning(false);
	}

	class MyIntentReceiver extends IntentReceiver {

		@Override
		public void onReceiveIntent(Context context, Intent intent) {

			if(intent.getAction().equals(ActivityID.MY_LOCATION_CHANGED_ACTION)){
			}

			if(intent.getAction().equals(ActivityID.STOPWATCH_STARTED)){
				Log.v(TAG, "!!!!! This is the intent Action :"+ intent.getAction().toString() );
				mapOfJogController.startTrackingRoute();
			}

			if(intent.getAction().equals(ActivityID.STOPWATCH_STOPPED)){
				Log.v(TAG, "!!!!! This is the intent Action :"+ intent.getAction().toString() );
				mapOfJogController.stopTrackingRoute();
			}

		}
	}


	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
	}

	@Override
	protected void onResume() {
		Handler msgHandler = mapOfJogController.getUpdateHandler();
		RemoteStopClockServiceInterface.getInstance().setUpdateHandler(msgHandler);

		mapOfJogController.onResume();
		
		super.onResume();
	}

	@Override
	protected void onPause() {
		RemoteStopClockServiceInterface.getInstance().clearUpdateHandler(mapOfJogController.getUpdateHandler());

		super.onPause();
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().activityStarted(this);
	}


	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().activityStopped(this);
	}
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		optionsMenu.selectItem(item);
	    return super.onOptionsItemSelected(item);
	}

}